<template>
  <a-card :bordered="false">

    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="日志ID">
              <a-input placeholder="请输入日志ID" v-model="queryParam.logId"></a-input>
            </a-form-item>
          </a-col>
          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="用户ID">
              <a-input placeholder="请输入用户ID" v-model="queryParam.userId"></a-input>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <a-form-item label="日志接口类型">
              <a-select
                showSearch
                placeholder="请选择日志接口类型"
                optionFilterProp="children"
                v-model="queryParam.logType"
              >
                <a-select-option v-for="d in templateDatas" :key="d.value" :value="d.value">{{d.text}}</a-select-option>
              </a-select>
            </a-form-item>
          </a-col>

          <a-col :xl="6" :lg="7" :md="8" :sm="24">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">查询</a-button>
              <a-button type="primary" @click="searchReset" icon="reload" style="margin-left: 8px">重置</a-button>
            </span>
          </a-col>

        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus" v-has="'litelog:add'">新增</a-button>
      <a-button type="primary" icon="download" @click="handleExportXls('API日志记录表')" v-has="'litelog:export'">导出
      </a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel" v-has="'litelog:import'">
        <a-button type="primary" icon="import">导入</a-button>
      </a-upload>
      <a-dropdown v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="1" @click="batchDel">
            <a-icon type="delete"/>
            删除
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px"> 批量操作
          <a-icon type="down"/>
        </a-button>
      </a-dropdown>
    </div>

    <!-- table区域-begin -->
    <div>

      <div class="ant-alert ant-alert-info" style="margin-bottom: 16px;">
        <i class="anticon anticon-info-circle ant-alert-icon"></i> 查询用户请求的日志，方便排查问题
      </div>

      <a-table
        ref="table"
        size="middle"
        bordered
        rowKey="id"
        :columns="columns"
        :dataSource="dataSource"
        :pagination="ipagination"
        :loading="loading"
        :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
        @change="handleTableChange">

        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">查看详情</a>
          <a-divider type="vertical"/>
          <a @click="handleParams(record)">请参详情</a>
          <a-divider type="vertical"/>
          <a @click="handleResponse(record)">返回详情</a>
        </span>
        <!-- 字符串超长截取省略号显示-->
        <span slot="longContent" slot-scope="text">
          <j-ellipsis :value="text" :length="32" />
        </span>
      </a-table>
    </div>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <liteApiLog-modal ref="modalForm" @ok="modalFormOk"></liteApiLog-modal>
    <liteApiLog-modal-params ref="modalParams" @ok="modalFormOk"></liteApiLog-modal-params>
    <liteApiLog-modal-response ref="modalResponse" @ok="modalFormOk"></liteApiLog-modal-response>
  </a-card>
</template>

<script>
  import LiteApiLogModal from './modules/LiteApiLogModal'
  import LiteApiLogModalParams from './modules/LiteApiLogModalParams'
  import LiteApiLogModalResponse from './modules/LiteApiLogModalResponse'
  import {JeecgListMixin} from '@/mixins/JeecgListMixin'
  import {ajaxGetDictItems} from '@/api/api'
  import JEllipsis from "@/components/jeecg/JEllipsis";
  const templateDatas=[] //接收下拉框数据的对象
  export default {
    name: "LiteApiLogList",
    mixins: [JeecgListMixin],
    components: {
      LiteApiLogModal,
      LiteApiLogModalParams,
      LiteApiLogModalResponse,
      JEllipsis
    },
    data() {
      return {
        description: 'API日志记录表管理页面',
        templateDatas,
        // 表头
        columns: [
          {
            title: '序号',
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: "center",
            customRender: function (t, r, index) {
              return parseInt(index) + 1;
            }
          },
          {
            title: '用户ID',
            align: "center",
            dataIndex: 'userId'
          },
          {
            title: '请求的IP',
            align: "center",
            dataIndex: 'requestIp',
            scopedSlots: {customRender: 'longContent'},
          },
          {
            title: 'IP归属地',
            align: "center",
            dataIndex: 'requestIp_dictText',
            scopedSlots: {customRender: 'longContent'},
          },
          {
            title: '请求的URL',
            align: "center",
            dataIndex: 'requestUrl'
          },
          {
            title: '日志接口类型',
            align: "center",
            dataIndex: 'logType_dictText'
          },
          {
            title: '耗时(ms)',
            align: "center",
            dataIndex: 'timeConsuming'
          },
          {
            title: '记录时间',
            align: "center",
            dataIndex: 'createTime'
          },
          {
            title: '操作',
            dataIndex: 'action',
            align: "center",
            scopedSlots: {customRender: 'action'},
          }
        ],
        url: {
          list: "/log/liteApiLog/list",
          delete: "/log/liteApiLog/delete",
          deleteBatch: "/log/liteApiLog/deleteBatch",
          exportXlsUrl: "log/liteApiLog/exportXls",
          importExcelUrl: "log/liteApiLog/importExcel",
        },
      }
    },
    computed: {
      importExcelUrl: function () {
        return `${window._CONFIG['domianURL']}/${this.url.importExcelUrl}`;
      }
    },
    created () {
      this.fetchTemplateData()
    },
    methods: {
      fetchTemplateData() {
        //根据字典Code, 初始化字典数组
        ajaxGetDictItems('api_type', null).then((res) => {
          if (res.success) {
            this.dictOptions = res.result;
            const result = res.result;
            result.forEach((r) => {
              templateDatas.push({
                value: r.value,
                text: r.text
              })
            })
          }
        })
      },
      handleParams: function (record) {
        this.$refs.modalParams.details(record);
        this.$refs.modalParams.title = "参数详情";
        this.$refs.modalParams.disableSubmit = false;
      },
      handleResponse: function (record) {
        this.$refs.modalResponse.details(record);
        this.$refs.modalResponse.title = "接口详情";
        this.$refs.modalResponse.disableSubmit = false;
      },
    }
  }
</script>
<style scoped>
  @import '~@assets/less/common.less'
</style>